package com.yangrong.thread.chapter10;

import java.util.Random;

public class ThreadLocalComplexText {

	private final static ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<String>();

	private final static Random random = new Random(System.currentTimeMillis());

	public static void main(String[] args) throws InterruptedException {
		Thread t1 = new Thread(() -> {
			THREAD_LOCAL.set("Thread-T1");
			try {
				Thread.sleep(random.nextInt(1000));
				System.out.println(Thread.currentThread().getName() + " " + THREAD_LOCAL.get());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});

		Thread t2 = new Thread(() -> {
			THREAD_LOCAL.set("Thread-T2");
			try {
				Thread.sleep(random.nextInt(1000));
				System.out.println(Thread.currentThread().getName() + " " + THREAD_LOCAL.get());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		});

		t1.start();
		t2.start();
		t1.join();
		t2.join();

		System.out.println(Thread.currentThread().getName() + " " + THREAD_LOCAL.get());
	}

}
